
package net.biaji.android.alephclient.account;

import net.biaji.alephclient.LibAgent;
import net.biaji.android.alephclient.AgentSingleton;
import net.biaji.android.alephclient.R;
import android.accounts.Account;
import android.accounts.AccountAuthenticatorActivity;
import android.accounts.AccountManager;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * 登录界面
 * 
 * @author biAji<biaji@biaji.net>
 */
public class LoginActivity extends AccountAuthenticatorActivity {

    private static final String TAG = "loginActivity";

    private EditText username, passwd;

    private TextView mErrorTag;

    private ProgressBar progress;

    private Button submit;

    private Context mContext;

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.login);
        username = (EditText) findViewById(R.id.username);
        passwd = (EditText) findViewById(R.id.passwd);
        mErrorTag = (TextView) findViewById(R.id.errortag);
        progress = (ProgressBar) findViewById(R.id.ongoing);
        submit = (Button) findViewById(R.id.submit);
        mContext = this;
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    public void login(View view) {

        String name = username.getText().toString();
        String passwd = this.passwd.getText().toString();
        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(passwd)) {
            mErrorTag.setText(R.string.login_err_empty_field);
            mErrorTag.setVisibility(View.VISIBLE);
            return;
        }
        new LoginTask().execute(name, passwd);

    }

    private class LoginTask extends AsyncTask<String, Integer, Boolean> {
        private LibAgent agent = AgentSingleton.getInstance();

        private String username = "", passwd = "";

        @Override
        protected void onPreExecute() {
            mErrorTag.setVisibility(View.GONE);
            submit.setVisibility(View.GONE);
            progress.setVisibility(View.VISIBLE);
        }

        @Override
        protected Boolean doInBackground(String... params) {
            username = params[0];
            passwd = params[1];
            return agent.login(username, passwd);
        }

        @Override
        protected void onPostExecute(Boolean result) {

            if (result) {

                AccountManager am = AccountManager.get(mContext);
                Account account = new Account(username, Authenticator.ACCOUNT_TYPE);
                am.addAccountExplicitly(account, passwd, null);
                Intent intent = new Intent();
                intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, result);
                setAccountAuthenticatorResult(intent.getExtras());
                setResult(RESULT_OK);
                finish();
            } else {
                mErrorTag.setText(R.string.login_err_failed);
                mErrorTag.setVisibility(View.VISIBLE);
                submit.setVisibility(View.VISIBLE);
                progress.setVisibility(View.GONE);
            }
        }
    }
}
